package com.sqx.modules.agentUserMoney.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.modules.agentCityDetails.entity.AgentCityDetails;
import com.sqx.modules.agentCityDetails.service.AgentCityDetailsService;
import com.sqx.modules.agentUserMoney.entity.AgentUserMoney;
import com.sqx.modules.agentUserMoney.dao.AgentUserMoneyDao;
import com.sqx.modules.agentUserMoney.service.AgentUserMoneyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.cityInfo.entity.CityInfo;
import com.sqx.modules.cityInfo.service.CityInfoService;
import com.sqx.modules.sys.entity.SysUserEntity;
import org.springframework.stereotype.Service;
import com.sqx.common.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 代理钱包 服务实现类
 * @author wuChang
 * @since 2024-03-22
 */
@Service
public class AgentUserMoneyServiceImp extends ServiceImpl<AgentUserMoneyDao, AgentUserMoney> implements AgentUserMoneyService {
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private AgentCityDetailsService agentCityDetailsService;

    @Override
    public Result saveAgentUserMoney(AgentUserMoney agentUserMoney) {
        if (agentUserMoney.getId() != null) {
            return Result.upStatus(baseMapper.updateById(agentUserMoney));
        } else {
            agentUserMoney.setCreateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(agentUserMoney));
        }
    }

    @Override
    public IPage<AgentUserMoney> getAgentUserMoneyList(Integer page, Integer limit, AgentUserMoney agentUserMoney) {
        Page<AgentUserMoney> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.selectPage(pages, new QueryWrapper<>(agentUserMoney).orderByAsc("create_time"));
    }

    @Override
    public Result updateUserMoney(Long sysUserId, Integer type, BigDecimal money) {
        AgentUserMoney agentUserMoney = baseMapper.selectOne(new QueryWrapper<AgentUserMoney>().eq("sys_user_id", sysUserId));
        if (type == 1) {
            agentUserMoney.setMoney(agentUserMoney.getMoney().add(money));
        } else {
            agentUserMoney.setMoney(agentUserMoney.getMoney().subtract(money));
        }
        CityInfo cityInfo = cityInfoService.getOne(new QueryWrapper<CityInfo>().eq("agent_user_id", sysUserId));

        baseMapper.updateById(agentUserMoney);
        AgentCityDetails agentCityDetails = new AgentCityDetails();
        agentCityDetails.setClassify(2);
        agentCityDetails.setMoney(money);
        agentCityDetails.setType(type);
        agentCityDetails.setSysUserId(sysUserId);
        if (cityInfo != null) {
            agentCityDetails.setCityId(cityInfo.getId());
        }
        agentCityDetails.setTitle("发起提现成功");
        if (type == 1) {
            agentCityDetails.setContent("管理员增加" + money + "元");
        } else {
            agentCityDetails.setContent("管理员扣款" + money + "元");
        }
        agentCityDetails.setCreateTime(LocalDateTime.now());
        agentCityDetailsService.save(agentCityDetails);

        return Result.success();
    }

    @Override
    public List<HashMap<String, Object>> getAgentRank(Integer page, Integer limit) {
        Page<HashMap<String, Object>> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getAgentRank(pages);


    }
}
